import { CurrentUser } from '@/decorators';
import {
  BankCreateDto,
  BankListDto,
  BankQueryDto,
  BankRemoveDto,
  BankRpaImportDto,
  BankTypeListDto,
} from '@/dtos';
import { BankService } from '@/services/bank.service';
import { ok } from '@/utils';
import { Body, Controller, Delete, Post, Query } from '@nestjs/common';
import { BaseController } from './base.controller';

@Controller('bank')
export class BankContorller extends BaseController {
  constructor(private readonly bankService: BankService) {
    super();
  }

  @Post('query')
  async query(@Body() dto: BankQueryDto) {
    return ok(await this.bankService.query(dto));
  }

  @Post('type/list')
  async type(@Body() dto: BankTypeListDto) {
    return ok(await this.bankService.getTypeList(dto));
  }

  @Post('list')
  async getList(@Body() dto: BankListDto, @CurrentUser('id') ownerId: number) {
    return ok(await this.bankService.getList(ownerId, dto));
  }

  @Post()
  async bankOpen(
    @Body() dto: BankCreateDto,
    @CurrentUser('id') ownerId: number,
  ) {
    return ok(
      await this.bankService.create(ownerId, {
        ...dto.data,
      }),
    );
  }

  @Delete()
  async bankClose(
    @Query() dto: BankRemoveDto,
    @CurrentUser('id') ownerId: number,
  ) {
    return ok(
      await this.bankService.remove({
        ...dto,
        ownerId,
      }),
    );
  }

  @Post('rpa/import')
  async rpaImport(@Body() dto: BankRpaImportDto) {
    await this.prisma.unionBank.createMany({
      data: dto.rows?.map((row) => ({
        ...row,
        bank: row.bank ?? row.name,
      })),
    });
    return ok(true);
  }
}
